home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / libdwarf / dwarf_form.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  11.2 KB  |  503 lines

  1. #include "dwarf_incl.h"
  2. #include "dwarf_die_deliv.h"
  3.  
  4. int
  5. dwarf_hasform (
  6.     Dwarf_Attribute    attr,
  7.     Dwarf_Half        form,
  8.     Dwarf_Bool          *return_bool,
  9.     Dwarf_Error        *error
  10. )
  11. {
  12.     if (attr == NULL) {
  13.     _dwarf_error(NULL,error,DW_DLE_ATTR_NULL);
  14.     return(DW_DLV_ERROR);
  15.     }
  16.  
  17.     if (attr->ar_cu_context == NULL) {
  18.     _dwarf_error(NULL,error,DW_DLE_ATTR_NO_CU_CONTEXT);
  19.     return(DW_DLV_ERROR);
  20.     }
  21.  
  22.     if (attr->ar_cu_context->cc_dbg == NULL) {
  23.     _dwarf_error(NULL,error,DW_DLE_ATTR_DBG_NULL);
  24.     return(DW_DLV_ERROR);
  25.     }
  26.  
  27.     *return_bool = (attr->ar_attribute_form == form);
  28.     return DW_DLV_OK;
  29. }
  30.  
  31.  
  32. int
  33. dwarf_whatform (
  34.     Dwarf_Attribute    attr,
  35.     Dwarf_Half      *   return_form,
  36.     Dwarf_Error        *error
  37. )
  38. {
  39.     if (attr == NULL) {
  40.     _dwarf_error(NULL,error,DW_DLE_ATTR_NULL);
  41.     return(DW_DLV_ERROR);
  42.     }
  43.  
  44.     if (attr->ar_cu_context == NULL) {
  45.     _dwarf_error(NULL,error,DW_DLE_ATTR_NO_CU_CONTEXT);
  46.     return(DW_DLV_ERROR);
  47.     }
  48.  
  49.     if (attr->ar_cu_context->cc_dbg == NULL) {
  50.     _dwarf_error(NULL,error,DW_DLE_ATTR_DBG_NULL);
  51.     return(DW_DLV_ERROR);
  52.     }
  53.  
  54.     *return_form = attr->ar_attribute_form;
  55.     return(DW_DLV_OK);
  56. }
  57.  
  58.  
  59. /*
  60.     This function is analogous to dwarf_whatform.
  61.     It returns the attribute in attr instead of
  62.     the form.
  63. */
  64. int
  65. dwarf_whatattr (
  66.     Dwarf_Attribute    attr,
  67.     Dwarf_Half      *   return_attr,
  68.     Dwarf_Error        *error
  69. )
  70. {
  71.     if (attr == NULL) {
  72.     _dwarf_error(NULL,error,DW_DLE_ATTR_NULL);
  73.     return(DW_DLV_ERROR);
  74.     }
  75.  
  76.     if (attr->ar_cu_context == NULL) {
  77.     _dwarf_error(NULL,error,DW_DLE_ATTR_NO_CU_CONTEXT);
  78.     return(DW_DLV_ERROR);
  79.     }
  80.  
  81.     if (attr->ar_cu_context->cc_dbg == NULL) {
  82.     _dwarf_error(NULL,error,DW_DLE_ATTR_DBG_NULL);
  83.     return(DW_DLV_ERROR);
  84.     }
  85.  
  86.     *return_attr = (attr->ar_attribute);
  87.     return DW_DLV_OK;
  88. }
  89.  
  90.  
  91. /* 
  92.     DW_FORM_ref_addr is considered an incorrect form 
  93.     for this call because this function returns an 
  94.     offset thru the pointer , 
  95.     and DW_FORM_ref_addr returns an address.
  96. */
  97. int
  98. dwarf_formref (
  99.     Dwarf_Attribute    attr,
  100.     Dwarf_Off    *      ret_offset,
  101.     Dwarf_Error        *error
  102. )
  103. {
  104.     Dwarf_Debug        dbg;
  105.     Dwarf_Unsigned    offset;
  106.  
  107.     if (attr == NULL) {
  108.     _dwarf_error(NULL,error,DW_DLE_ATTR_NULL);
  109.     return(DW_DLV_ERROR);
  110.     }
  111.  
  112.     if (attr->ar_cu_context == NULL) {
  113.     _dwarf_error(NULL,error,DW_DLE_ATTR_NO_CU_CONTEXT);
  114.     return(DW_DLV_ERROR);
  115.     }
  116.  
  117.     if (attr->ar_cu_context->cc_dbg == NULL) {
  118.     _dwarf_error(NULL,error,DW_DLE_ATTR_DBG_NULL);
  119.     return(DW_DLV_ERROR);
  120.     }
  121.     dbg = attr->ar_cu_context->cc_dbg;
  122.  
  123.     switch (attr->ar_attribute_form) {
  124.  
  125.     case DW_FORM_ref1 : 
  126.         offset = *(Dwarf_Small *)attr->ar_debug_info_ptr;
  127.         break;
  128.  
  129.     case DW_FORM_ref2 : 
  130.         READ_UNALIGNED(offset, attr->ar_debug_info_ptr, sizeof(Dwarf_Half));
  131.         break;
  132.  
  133.     case DW_FORM_ref4 : 
  134.         READ_UNALIGNED(offset, attr->ar_debug_info_ptr, sizeof(Dwarf_Word));
  135.         break;
  136.  
  137.     case DW_FORM_ref8 : 
  138.         READ_UNALIGNED(offset, attr->ar_debug_info_ptr, 
  139.         sizeof(Dwarf_Unsigned));
  140.         break;
  141.  
  142.     case DW_FORM_ref_udata :
  143.         offset = _dwarf_decode_u_leb128(attr->ar_debug_info_ptr,NULL);
  144.         break;
  145.  
  146.     default :
  147.         _dwarf_error(dbg,error,DW_DLE_BAD_REF_FORM);
  148.         return(DW_DLV_ERROR);
  149.     }
  150.  
  151.     /* Check that offset is within current cu portion of .debug_info. */
  152.     if (offset >= attr->ar_cu_context->cc_length + dbg->de_length_size) {
  153.     _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_OFFSET_BAD);
  154.     return(DW_DLV_ERROR);
  155.     }
  156.  
  157.     *ret_offset = (offset);
  158.     return DW_DLV_OK;
  159. }
  160.  
  161.  
  162. int
  163. dwarf_formaddr (
  164.     Dwarf_Attribute    attr,
  165.     Dwarf_Addr       *  return_addr,
  166.     Dwarf_Error        *error
  167. )
  168. {
  169.     Dwarf_Debug        dbg;
  170.     Dwarf_Addr        ret_addr;
  171.  
  172.     if (attr == NULL) {
  173.     _dwarf_error(NULL,error,DW_DLE_ATTR_NULL);
  174.     return(DW_DLV_ERROR);
  175.     }
  176.  
  177.     if (attr->ar_cu_context == NULL) {
  178.     _dwarf_error(NULL,error,DW_DLE_ATTR_NO_CU_CONTEXT);
  179.     return(DW_DLV_ERROR);
  180.     }
  181.  
  182.     if (attr->ar_cu_context->cc_dbg == NULL) {
  183.     _dwarf_error(NULL,error,DW_DLE_ATTR_DBG_NULL);
  184.     return(DW_DLV_ERROR);
  185.     }
  186.     dbg = attr->ar_cu_context->cc_dbg;
  187.  
  188.     if (attr->ar_attribute_form == DW_FORM_addr ||
  189.     attr->ar_attribute_form == DW_FORM_ref_addr) {
  190.     READ_UNALIGNED(ret_addr, attr->ar_debug_info_ptr, dbg->de_length_size);
  191.     *return_addr = ret_addr;
  192.         return(DW_DLV_OK);
  193.     }
  194.  
  195.     _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_BAD);
  196.     return(DW_DLV_ERROR);
  197. }
  198.  
  199.  
  200. int
  201. dwarf_formflag (
  202.     Dwarf_Attribute    attr,
  203.     Dwarf_Bool    *     ret_bool,
  204.     Dwarf_Error        *error
  205. )
  206. {
  207.     if (attr == NULL) {
  208.     _dwarf_error(NULL,error,DW_DLE_ATTR_NULL);
  209.     return(DW_DLV_ERROR);
  210.     }
  211.  
  212.     if (attr->ar_cu_context == NULL) {
  213.     _dwarf_error(NULL,error,DW_DLE_ATTR_NO_CU_CONTEXT);
  214.     return(DW_DLV_ERROR);
  215.     }
  216.  
  217.     if (attr->ar_cu_context->cc_dbg == NULL) {
  218.     _dwarf_error(NULL,error,DW_DLE_ATTR_DBG_NULL);
  219.     return(DW_DLV_ERROR);
  220.     }
  221.  
  222.     if (attr->ar_attribute_form == DW_FORM_flag) {
  223.     *ret_bool = (*(Dwarf_Small *)attr->ar_debug_info_ptr != 0);
  224.     return(DW_DLV_OK);
  225.     }
  226.     _dwarf_error(attr->ar_cu_context->cc_dbg,error,DW_DLE_ATTR_FORM_BAD);
  227.     return(DW_DLV_ERROR);
  228. }
  229.  
  230.  
  231. int 
  232. dwarf_formudata (
  233.     Dwarf_Attribute    attr,
  234.     Dwarf_Unsigned     *return_uval,
  235.     Dwarf_Error        *error
  236. )
  237. {
  238.     Dwarf_Unsigned    ret_value;
  239.  
  240.     if (attr == NULL) {
  241.     _dwarf_error(NULL,error,DW_DLE_ATTR_NULL);
  242.     return(DW_DLV_ERROR);
  243.     }
  244.  
  245.  
  246.     if (attr->ar_cu_context == NULL) {
  247.     _dwarf_error(NULL,error,DW_DLE_ATTR_NO_CU_CONTEXT);
  248.     return(DW_DLV_ERROR);
  249.     }
  250.  
  251.     if (attr->ar_cu_context->cc_dbg == NULL) {
  252.     _dwarf_error(NULL,error,DW_DLE_ATTR_DBG_NULL);
  253.     return(DW_DLV_ERROR);
  254.     }
  255.  
  256.     switch (attr->ar_attribute_form) {
  257.  
  258.     case DW_FORM_data1 :
  259.         READ_UNALIGNED(ret_value, attr->ar_debug_info_ptr, 
  260.         sizeof(Dwarf_Small));
  261.             *return_uval = ret_value;
  262.         return DW_DLV_OK;
  263.  
  264.     case DW_FORM_data2 : {
  265.         READ_UNALIGNED(ret_value, attr->ar_debug_info_ptr, 
  266.         sizeof(Dwarf_Half));
  267.             *return_uval = ret_value;
  268.         return DW_DLV_OK;
  269.     }
  270.  
  271.     case DW_FORM_data4 : {
  272.         READ_UNALIGNED(ret_value, attr->ar_debug_info_ptr, 
  273.         sizeof(Dwarf_Word));
  274.             *return_uval = ret_value;
  275.         return DW_DLV_OK;
  276.     }
  277.  
  278.     case DW_FORM_data8 : {
  279.         READ_UNALIGNED(ret_value, attr->ar_debug_info_ptr, 
  280.         sizeof(Dwarf_Unsigned));
  281.             *return_uval = ret_value;
  282.         return DW_DLV_OK;
  283.     }
  284.  
  285.     case DW_FORM_udata :
  286.         ret_value = (_dwarf_decode_u_leb128(attr->ar_debug_info_ptr,NULL));
  287.             *return_uval = ret_value;
  288.         return DW_DLV_OK;
  289.  
  290.     default :
  291.         break;
  292.     }
  293.     _dwarf_error(attr->ar_cu_context->cc_dbg,error,
  294.         DW_DLE_ATTR_FORM_BAD);
  295.     return(DW_DLV_ERROR);
  296. }
  297.  
  298.  
  299. int
  300. dwarf_formsdata (
  301.     Dwarf_Attribute    attr,
  302.     Dwarf_Signed     *  return_sval,
  303.     Dwarf_Error        *error
  304. )
  305. {
  306.     Dwarf_Signed    ret_value;
  307.  
  308.     if (attr == NULL) {
  309.     _dwarf_error(NULL,error,DW_DLE_ATTR_NULL);
  310.     return(DW_DLV_ERROR);
  311.     }
  312.  
  313.     if (attr->ar_cu_context == NULL) {
  314.     _dwarf_error(NULL,error,DW_DLE_ATTR_NO_CU_CONTEXT);
  315.     return(DW_DLV_ERROR);
  316.     }
  317.  
  318.     if (attr->ar_cu_context->cc_dbg == NULL) {
  319.     _dwarf_error(NULL,error,DW_DLE_ATTR_DBG_NULL);
  320.     return(DW_DLV_ERROR);
  321.     }
  322.  
  323.     switch (attr->ar_attribute_form) {
  324.  
  325.     case DW_FORM_data1 :
  326.         *return_sval = (*(Dwarf_Sbyte *)attr->ar_debug_info_ptr);
  327.         return DW_DLV_OK;
  328.  
  329.     case DW_FORM_data2 : {
  330.         READ_UNALIGNED(ret_value, attr->ar_debug_info_ptr, 
  331.         sizeof(Dwarf_Shalf));
  332.         *return_sval = ret_value;
  333.         return DW_DLV_OK;
  334.     
  335.     }
  336.  
  337.     case DW_FORM_data4 : {
  338.         READ_UNALIGNED(ret_value, attr->ar_debug_info_ptr, 
  339.         sizeof(Dwarf_Sword));
  340.         *return_sval = ret_value;
  341.         return DW_DLV_OK;
  342.     }
  343.  
  344.     case DW_FORM_data8 : {
  345.         READ_UNALIGNED(ret_value, attr->ar_debug_info_ptr, 
  346.         sizeof(Dwarf_Signed));
  347.         *return_sval = ret_value;
  348.         return DW_DLV_OK;
  349.     }
  350.  
  351.     case DW_FORM_sdata :
  352.         ret_value = (_dwarf_decode_s_leb128(attr->ar_debug_info_ptr,NULL));
  353.         *return_sval = ret_value;
  354.         return DW_DLV_OK;
  355.  
  356.     default :
  357.        break;
  358.     }
  359.     _dwarf_error(attr->ar_cu_context->cc_dbg,error,
  360.     DW_DLE_ATTR_FORM_BAD);
  361.     return(DW_DLV_ERROR);
  362. }
  363.  
  364.  
  365. int
  366. dwarf_formblock (
  367.     Dwarf_Attribute    attr,
  368.     Dwarf_Block    **   return_block,
  369.     Dwarf_Error        *error
  370. )
  371. {
  372.     Dwarf_CU_Context    cu_context;
  373.     Dwarf_Debug        dbg;
  374.     Dwarf_Unsigned    length;
  375.     Dwarf_Small        *data;
  376.     Dwarf_Word        leb128_length;
  377.     Dwarf_Block        *ret_block;
  378.  
  379.     if (attr == NULL) {
  380.     _dwarf_error(NULL,error,DW_DLE_ATTR_NULL);
  381.     return(DW_DLV_ERROR);
  382.     }
  383.  
  384.     if (attr->ar_cu_context == NULL) {
  385.     _dwarf_error(NULL,error,DW_DLE_ATTR_NO_CU_CONTEXT);
  386.     return(DW_DLV_ERROR);
  387.     }
  388.     cu_context = attr->ar_cu_context;
  389.  
  390.     if (cu_context->cc_dbg == NULL) {
  391.     _dwarf_error(NULL,error,DW_DLE_ATTR_DBG_NULL);
  392.     return(DW_DLV_ERROR);
  393.     }
  394.     dbg = cu_context->cc_dbg;
  395.  
  396.     switch (attr->ar_attribute_form) {
  397.  
  398.     case DW_FORM_block1 :
  399.         length = *(Dwarf_Small *)attr->ar_debug_info_ptr;
  400.         data = attr->ar_debug_info_ptr + sizeof(Dwarf_Small);
  401.         break;
  402.     
  403.     case DW_FORM_block2 :
  404.         READ_UNALIGNED(length, attr->ar_debug_info_ptr, sizeof(Dwarf_Half));
  405.         data = attr->ar_debug_info_ptr + sizeof(Dwarf_Half);
  406.         break;
  407.  
  408.     case DW_FORM_block4 :
  409.         READ_UNALIGNED(length, attr->ar_debug_info_ptr, sizeof(Dwarf_Word));
  410.         data = attr->ar_debug_info_ptr + sizeof(Dwarf_Word);
  411.         break;
  412.  
  413.     case DW_FORM_block :
  414.         length = _dwarf_decode_u_leb128(attr->ar_debug_info_ptr, 
  415.         &leb128_length);
  416.         data = attr->ar_debug_info_ptr + leb128_length;
  417.         break;
  418.  
  419.     default :
  420.         _dwarf_error(cu_context->cc_dbg,error,DW_DLE_ATTR_FORM_BAD);
  421.         return(DW_DLV_ERROR);
  422.         break;
  423.     }
  424.  
  425.     /* Check that block lies within current cu in .debug_info. */
  426.     if (attr->ar_debug_info_ptr + length >=
  427.     dbg->de_debug_info + cu_context->cc_debug_info_offset + 
  428.     cu_context->cc_length + dbg->de_length_size) {
  429.         _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_SIZE_BAD);
  430.         return(DW_DLV_ERROR);
  431.     }
  432.  
  433.     ret_block = (Dwarf_Block *)_dwarf_get_alloc(dbg, DW_DLA_BLOCK, 1);
  434.     if (ret_block == NULL) {
  435.     _dwarf_error(dbg,error,DW_DLE_ALLOC_FAIL);
  436.     return(DW_DLV_ERROR);
  437.     }
  438.  
  439.     ret_block->bl_len = length;
  440.     ret_block->bl_data = (Dwarf_Ptr)data;
  441.  
  442.     *return_block = ret_block;
  443.     return(DW_DLV_OK);
  444. }
  445.  
  446.  
  447. int
  448. dwarf_formstring (
  449.     Dwarf_Attribute    attr,
  450.     char **             return_str,
  451.     Dwarf_Error        *error
  452. )
  453. {
  454.     Dwarf_CU_Context    cu_context;
  455.     Dwarf_Debug        dbg;
  456.     Dwarf_Unsigned    offset;
  457.  
  458.     if (attr == NULL) {
  459.     _dwarf_error(NULL,error,DW_DLE_ATTR_NULL);
  460.     return(DW_DLV_ERROR);
  461.     }
  462.  
  463.     if (attr->ar_cu_context == NULL) {
  464.     _dwarf_error(NULL,error,DW_DLE_ATTR_NO_CU_CONTEXT);
  465.     return(DW_DLV_ERROR);
  466.     }
  467.     cu_context = attr->ar_cu_context;
  468.  
  469.     if (cu_context->cc_dbg == NULL) {
  470.     _dwarf_error(NULL,error,DW_DLE_ATTR_DBG_NULL);
  471.     return(DW_DLV_ERROR);
  472.     }
  473.     dbg = cu_context->cc_dbg;
  474.  
  475.     if (attr->ar_attribute_form == DW_FORM_string) {
  476.  
  477.         /* Check that string lies within current cu in .debug_info. */
  478.     if ((attr->ar_debug_info_ptr + strlen(attr->ar_debug_info_ptr) + 1) >= 
  479.         (dbg->de_debug_info + cu_context->cc_debug_info_offset + 
  480.         cu_context->cc_length + dbg->de_length_size)) {
  481.         _dwarf_error(dbg, error, DW_DLE_ATTR_FORM_SIZE_BAD);
  482.         return(DW_DLV_ERROR);
  483.     }
  484.     *return_str = (attr->ar_debug_info_ptr);
  485.         return DW_DLV_OK;
  486.     }
  487.  
  488.     if (attr->ar_attribute_form == DW_FORM_strp) {
  489.     READ_UNALIGNED(offset, attr->ar_debug_info_ptr, dbg->de_length_size);
  490.  
  491.     if (dbg->de_debug_str == NULL) {
  492.         _dwarf_error(dbg, error, DW_DLE_DEBUG_STR_NULL);
  493.         return(DW_DLV_ERROR);
  494.     }
  495.     *return_str = (dbg->de_debug_str + offset);
  496.         return DW_DLV_OK;
  497.     }
  498.  
  499.     _dwarf_error(dbg,error,DW_DLE_ATTR_FORM_BAD);
  500.     return(DW_DLV_ERROR);
  501. }
  502.  
  503.